package hydrap2p.gui.commands;

import hydrap2p.gui.FileTransferPanel;
import hydrap2p.gui.GUIDataHandler;
import hydrap2p.gui.SearchPanel;
import hydrap2p.gui.StartupManagerPanel;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public class Cmd_StartupManagerPanel_RemoveModule implements Command {

	@Override
	public void execute() {
		List<Integer> indexList = new ArrayList<Integer>();
		
		// Put selected indices in a sortable list
		int[] indices = StartupManagerPanel.getInstance().getTable().getSelectedRows();
		for(int i : indices){
			indexList.add(i);
		}
		
		// Sort the indices in descending order
		Collections.sort(indexList);
		Collections.reverse(indexList);
		
		// Remove the indices from the download listing
		Map<Integer, Integer> tableMap = FileTransferPanel.getInstance().getIndexMappings();
		int gdlIndex;
		for(int i : indexList){
			//System.out.println(tableMap.size() + " " + i);
			//gdlIndex = tableMap.get(i);
			//System.out.println("Size: " + GUIDataHandler.getInstance().getDownloadListing().size() + ", Order: " + i + ", Index: " + gdlIndex);
			
			//GUIDataHandler.getInstance().getModules().remove(i);
			GUIDataHandler.getInstance().removeModule(i);
			
			// Refresh data transfer table
			StartupManagerPanel.getInstance().refreshModuleList();
			
			//refresh search plugins in case one was removed
			SearchPanel.getInstance().refreshPlugins();
		}
		
		
	}

	@Override
	public String getCommand() {
		return this.getClass().getSimpleName();
	}

	@Override
	public void unexecute() {
		// TODO Auto-generated method stub

	}

}
